/***********************************************************
*discription    mainWindow示例
*author         david
*date           2021-5-8
*modify         2021-5-8 8：30
************************************************************/
#include "mainWindow.h"

mainWindow::mainWindow()
{
	setWindowTitle("haha");
	textEdit = new QTextEdit();
	//设置中心部件
	setCentralWidget(textEdit);
	//创建动作
	createActions();
	//创建菜单
	createMenus();
	//创建工具条
	createToolBar();
	//设置状态栏
	setStatusBar();
	//创建锚接部件
	createDockWidget();
}

void mainWindow::openSlot()
{
    textEdit->setText(tr("打开文件"));
}
void mainWindow::saveSlot()
{
    textEdit->setText(tr("保存文件"));
}
void mainWindow::exitSlot()
{
    textEdit->setText(tr("退出"));
}
void mainWindow::copySlot()
{
    textEdit->setText(tr("复制"));
}
void mainWindow::cutSlot()
{
    textEdit->setText(tr("剪切"));
}
void mainWindow::pasteSlot()
{
    textEdit->setText(tr("粘贴"));
}

void mainWindow::createActions()
{
	//创建QAction（资源，名字，父窗口）
	openAct = new QAction(QIcon(":/images/open.png"), tr("打开"), this);
	//快捷键
	openAct->setShortcut(QKeySequence(tr("ctrl+o")));
	//设置状态栏提示
	openAct->setStatusTip(tr("打开文件"));
	//关联信号和槽函数
	connect(openAct,SIGNAL(triggered()), this, SLOT(openSlot()));

    //保存
    saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"),this);
    saveAct->setShortcut(QKeySequence(tr("ctrl+s")));
    saveAct->setStatusTip(tr("保存文件"));
    connect(saveAct,SIGNAL(triggered()), this, SLOT(saveSlot()));

    //剪切
    cutAct = new QAction(QIcon(":/images/cut.png"), tr("&Cut"),this);
    cutAct->setShortcut(QKeySequence(tr("ctrl+c")));
    cutAct->setStatusTip(tr("打开文件"));
    connect(cutAct,SIGNAL(triggered()), this, SLOT(cutSlot()));

    //复制
    copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"),this);
    copyAct->setShortcut(QKeySequence(tr("ctrl+v")));
    copyAct->setStatusTip(tr("复制"));
    connect(copyAct,SIGNAL(triggered()), this, SLOT(copySlot()));

    //粘贴
    pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"),this);
    pasteAct->setShortcut(QKeySequence(tr("ctrl+a")));
    pasteAct->setStatusTip(tr("粘贴"));
    connect(pasteAct,SIGNAL(triggered()), this, SLOT(pasteSlot()));

    //关于
    aboutAct = new QAction(QIcon(":/images/about.png"), tr("&About"),this);
    aboutAct->setShortcut(QKeySequence(tr("ctrl+o")));
    aboutAct->setStatusTip(tr("关于"));
    connect(aboutAct,SIGNAL(triggered()), this, SLOT(aboutSlot()));

}
void mainWindow::createMenus()
{
    //如果没有这句，不会显示菜单栏
    menuBar()->setNativeMenuBar(false);

    //文件
    fileMenu = new QMenu(tr("&File"),this);
	//为菜单添加动作
	fileMenu->addAction(openAct);
    fileMenu->addAction(saveAct);
    //添加菜单
    this->menuBar()->addMenu(fileMenu);

    //编辑
    editMenu = new QMenu(tr("&Edit"),this);
    //为菜单添加动作
    editMenu->addAction(cutAct);
    editMenu->addAction(copyAct);
    editMenu->addAction(pasteAct);
    //添加菜单
    this->menuBar()->addMenu(editMenu);

    //帮助
    helpMenu = new QMenu(tr("&Help"),this);
    //为菜单添加动作
    helpMenu->addAction(aboutAct);
    //添加菜单
    this->menuBar()->addMenu(helpMenu);

}
void mainWindow::createToolBar()
{
	//添加工具栏
	fileToolBar = addToolBar(tr("文件"));
	//为工具栏添加动作
	fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);
    fileToolBar->addAction(cutAct);
    fileToolBar->addAction(copyAct);
    fileToolBar->addAction(pasteAct);
}
void mainWindow::setStatusBar()
{
	//设置状态栏信息
	statusBar()->showMessage(tr("准备"));
}
void mainWindow::createDockWidget()
{
	//创建锚接部件
	dock = new QDockWidget(tr("锚接窗口"), this);
	//向锚接部件添加控件
	text = new QTextEdit();
	dock->setWidget(text);
	//将锚接部件加到主窗口
	addDockWidget(Qt::RightDockWidgetArea, dock);
}
